package com.yy.wga.user.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.wga.common.core.dto.BaseFeignApiReqDTO;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.core.enmus.EUserGrade;
import com.yy.wga.user.feign.api.UserRelationSummaryFeign;
import com.yy.wga.user.feign.response.UserRelationSubCountResp;
import com.yy.wga.user.service.pojo.UserRelationDO;
import com.yy.wga.user.service.pojo.UserRelationSummaryDO;
import com.yy.wga.user.service.service.UserRelationService;
import com.yy.wga.user.service.service.UserRelationSummaryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户粉丝数据汇总
 *
 * @author guochao
 * @since 2025-01-04 18:34:28
 */
@RestController
@RequestMapping("api/user/relationSummary")
public class UserRelationSummaryController implements UserRelationSummaryFeign {
    @Resource
    private UserRelationSummaryService userRelationSummaryService;
    @Resource
    private UserRelationService userRelationService;

    @Override
    @PostMapping("subFansCount")
    public Result<UserRelationSubCountResp> subFansCount(@RequestBody @Valid BaseFeignApiReqDTO reqDTO) {
        UserRelationSummaryDO userRelationSummaryDO = userRelationSummaryService.getOne(new LambdaQueryWrapper<UserRelationSummaryDO>()
                .eq(UserRelationSummaryDO::getAppKey, reqDTO.getAppKey())
                .eq(UserRelationSummaryDO::getUserId, reqDTO.getUserId()));

        List<UserRelationDO> userRelationList = userRelationService.list(new LambdaQueryWrapper<UserRelationDO>()
                .eq(UserRelationDO::getAppKey, reqDTO.getAppKey())
                .eq(UserRelationDO::getParentUserId, reqDTO.getUserId()));
        UserRelationSubCountResp response = new UserRelationSubCountResp();
        response.setSubDirectFansNum(userRelationList.size());
        response.setSubShareNum((int) userRelationList.stream()
                .filter(r -> r.getGrade().equals(EUserGrade.SHARE_1.getType()))
                .count());
        response.setSubServerNum((int) userRelationList.stream()
                .filter(r -> r.getGrade().equals(EUserGrade.SERVER_1.getType()))
                .count());
        if (userRelationSummaryDO != null) {
            response.setSubFansNum(userRelationSummaryDO.getSubCount());
        }
        return Result.success(response);
    }
}
